package com.yljk.mydoctor.fragment;


import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.yilijk.base.base.BaseFragment;
import com.yilijk.base.constants.UserInfo;
import com.yilijk.base.databinding.DataBindingRecyclerViewAdapter;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.SharedPreferencesUtils;
import com.yilijk.base.utils.ToastUtils;
import com.yljk.mydoctor.BR;
import com.yljk.mydoctor.R;
import com.yljk.mydoctor.databinding.MineFragmentDataBinding;
import com.yljk.mydoctor.entity.NewMineBean;
import com.yljk.mydoctor.utils.MineUtils;
import com.yljk.mydoctor.utils.PopWindowUtils;
import com.yljk.mydoctor.viewmodel.MineViewModel;
import com.yljk.servicemanager.constants.ClickApi;
import com.yljk.servicemanager.constants.RouterApi;

import java.util.ArrayList;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2021/12/07
 */

@Route(path = RouterApi.NewMineFragment)
public class NewMineDoctorFragment extends BaseFragment<MineViewModel, MineFragmentDataBinding> {

    private DataBindingRecyclerViewAdapter topAdapter;
    private ArrayList<NewMineBean.DataBean.TopDTOBean.IconDTOListBean> topList = new ArrayList();

    private DataBindingRecyclerViewAdapter serverAdapter;
    private ArrayList<NewMineBean.DataBean.MiddleDTOListBean.IconDTOListBean> serverList = new ArrayList();

    private DataBindingRecyclerViewAdapter commonAdapter;
    private ArrayList<NewMineBean.DataBean.MiddleDTOListBean.IconDTOListBean> commonList = new ArrayList();

    private DataBindingRecyclerViewAdapter renzAdapter;
    private ArrayList<NewMineBean.DataBean.MiddleDTOListBean.IconDTOListBean> renzList = new ArrayList();

    private DataBindingRecyclerViewAdapter bottomAdapter;
    private ArrayList<NewMineBean.DataBean.BottomDTOBean.IconDTOListBean> bottomList = new ArrayList();


    @Override
    protected int getContentViewId() {
        return R.layout.layout_fragment_mine;
    }


    @Override
    protected void processLogic(Bundle savedInstanceState) {

        ClickUtils.sendEvent(ClickApi.Mine.Mine_Load, getActivity(), "我的载入");
        mViewModel.initPopWindow(getActivity());
        PopWindowUtils.getInstance().init(getActivity());
        topAdapter = new DataBindingRecyclerViewAdapter(getActivity(), R.layout.layout_fragment_item_type0, BR.TopBeanItem, topList);
        binding.topRv.setLayoutManager(new GridLayoutManager(getContext(), 3));
        binding.topRv.setAdapter(topAdapter);
        topAdapter.notifyDataSetChanged();

        serverAdapter = new DataBindingRecyclerViewAdapter(getActivity(), R.layout.layout_fragment_item_type2, BR.MiddleBeanItem, serverList);
        binding.serverRv.setLayoutManager(new GridLayoutManager(getContext(), 4));
        binding.serverRv.setAdapter(serverAdapter);
        serverAdapter.notifyDataSetChanged();

        commonAdapter = new DataBindingRecyclerViewAdapter(getActivity(), R.layout.layout_fragment_item_type1, BR.MiddleBeanItem, commonList);
        binding.commonRv.setLayoutManager(new GridLayoutManager(getContext(), 4));
        binding.commonRv.setAdapter(commonAdapter);
        commonAdapter.notifyDataSetChanged();

        renzAdapter = new DataBindingRecyclerViewAdapter(getActivity(), R.layout.layout_fragment_item_type3, BR.MiddleBeanItem, renzList);
        binding.renzRv.setLayoutManager(new GridLayoutManager(getContext(), 4));
        binding.renzRv.setAdapter(renzAdapter);
        renzAdapter.notifyDataSetChanged();

        bottomAdapter = new DataBindingRecyclerViewAdapter(getActivity(), R.layout.layout_fragment_item_type4, BR.BottomBeanItem, bottomList);
        binding.bottRv.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.bottRv.setAdapter(bottomAdapter);
        bottomAdapter.notifyDataSetChanged();

    }


    @Override
    public void onResume() {
        super.onResume();
        mViewModel.initMineUI().observe(this, dataBeanResource -> {
            if (dataBeanResource.data != null) {
                if (dataBeanResource.data.getMyDTO() != null) {
                    binding.setMyDTO(dataBeanResource.data.getMyDTO());
                    if (dataBeanResource.data.getMyDTO().getDoctorInsuranceIcon() != null &&
                            !TextUtils.isEmpty(dataBeanResource.data.getMyDTO().getDoctorInsuranceIcon().getPictureUrl())) {
                        binding.insurance.setVisibility(VISIBLE);
                    } else {
                        binding.insurance.setVisibility(GONE);
                    }
                    if (!TextUtils.isEmpty(dataBeanResource.data.getMyDTO().getSupportPictureUrl())) {
                        binding.supportPicture.setVisibility(VISIBLE);
                    } else {
                        binding.supportPicture.setVisibility(GONE);
                    }
                    if (!TextUtils.isEmpty(dataBeanResource.data.getMyDTO().getName())) {
                        binding.baseInfo.setVisibility(GONE);
                        binding.detailInfo.setVisibility(VISIBLE);
                        SharedPreferencesUtils.getInstance().save(UserInfo.userName, dataBeanResource.data.getMyDTO().getName());
                    }
                    if (!TextUtils.isEmpty(dataBeanResource.data.getMyDTO().getTitle())) {
                        SharedPreferencesUtils.getInstance().save(UserInfo.JobtitleDesc, dataBeanResource.data.getMyDTO().getTitle());
                    }

                } else {
                    binding.baseInfo.setVisibility(VISIBLE);
                    binding.detailInfo.setVisibility(GONE);
                }

                binding.topRv.setVisibility(GONE);
                if (dataBeanResource.data.getTopDTO() != null && dataBeanResource.data.getTopDTO().getIconDTOList() != null) {
                    topList.clear();
                    topList.addAll(dataBeanResource.data.getTopDTO().getIconDTOList());
                    binding.setTopDTOList(topList);
                    binding.topRv.setLayoutManager(new GridLayoutManager(getContext(), dataBeanResource.data.getTopDTO().getCell()));
                    binding.topRv.setLayoutManager(new GridLayoutManager(getContext(), topList.size()));
                    topAdapter.notifyDataSetChanged();
                    binding.topRv.setVisibility(VISIBLE);
                }
                binding.serverCv.setVisibility(GONE);
                binding.commonCv.setVisibility(GONE);
                binding.renzCv.setVisibility(GONE);
                if (dataBeanResource.data.getMiddleDTOList() != null) {
                    for (int i = 0; i < dataBeanResource.data.getMiddleDTOList().size(); i++) {
                        NewMineBean.DataBean.MiddleDTOListBean middleDTOListBean = dataBeanResource.data.getMiddleDTOList().get(i);
                        switch (dataBeanResource.data.getMiddleDTOList().get(i).getFunctionalClassificationCode()) {
                            case "myService":
                                binding.serverCv.setVisibility(VISIBLE);
                                //设置我的服务主题
                                binding.serverTv.setText(middleDTOListBean.getFunctionalClassification());
                                serverList.clear();
                                serverList.addAll(middleDTOListBean.getIconDTOList());
                                binding.serverRv.setLayoutManager(new GridLayoutManager(getContext(), middleDTOListBean.getCell()));
                                binding.serverRv.setAdapter(serverAdapter);
                                serverAdapter.notifyDataSetChanged();
                                break;
                            case "commonTools":
                                binding.commonCv.setVisibility(VISIBLE);
                                //设置常用工具主题
                                binding.commonTv.setText(middleDTOListBean.getFunctionalClassification());
                                commonList.clear();
                                commonList.addAll(middleDTOListBean.getIconDTOList());
                                binding.commonRv.setLayoutManager(new GridLayoutManager(getContext(), middleDTOListBean.getCell()));
                                binding.commonRv.setAdapter(commonAdapter);
                                commonAdapter.notifyDataSetChanged();
                                break;
                            case "myCertification":
                                binding.renzCv.setVisibility(VISIBLE);
                                //设置我的认证主题
                                binding.renzTv.setText(middleDTOListBean.getFunctionalClassification());
                                renzList.clear();
                                renzList.addAll(middleDTOListBean.getIconDTOList());
                                binding.renzRv.setLayoutManager(new GridLayoutManager(getContext(), middleDTOListBean.getCell()));
                                binding.renzRv.setAdapter(renzAdapter);
                                renzAdapter.notifyDataSetChanged();
                                break;
                        }
                    }
                }

                if (dataBeanResource.data.getBottomDTO() != null && dataBeanResource.data.getBottomDTO().getIconDTOList() != null) {
                    bottomList.clear();
                    bottomList.addAll(dataBeanResource.data.getBottomDTO().getIconDTOList());
                    if (bottomList.size() > 0) {
                        bottomList.remove(bottomList.size() - 1);
                    }
                    binding.bottRv.setLayoutManager(new LinearLayoutManager(getContext()));
                    binding.bottRv.setAdapter(bottomAdapter);
                    bottomAdapter.notifyDataSetChanged();
                }


            }
        });
    }

    @Override
    protected void setListener() {
        topAdapter.setOnItemClickListener((view, position) -> mViewModel.push(getActivity(), topList.get(position).getName(), topList.get(position).getRnModuleNew(), topList.get(position).getRnPageNew(), null));
        serverAdapter.setOnItemClickListener((view, position) -> mViewModel.push(getActivity(), serverList.get(position).getName(), serverList.get(position).getRnModuleNew(), serverList.get(position).getRnPageNew(), null));
        commonAdapter.setOnItemClickListener((view, position) -> {
            NewMineBean.DataBean.MiddleDTOListBean.IconDTOListBean listBean = commonList.get(position);
            Bundle bundle = new Bundle();
            bundle.putString("id", binding.getMyDTO().getPassportId() + "");
            bundle.putBoolean("isShare", binding.getMyDTO().isShare());
            if (!TextUtils.isEmpty(listBean.getRnPageNew()) && listBean.getRnPageNew().contains("jumpToLive")) {
                //TODO 跳转医学证据库
                //com.doctor.scheme://doctor/jumpToLive?page=evidenceBase
                ALog.e("onMinViewClick", "rnPageName: " + listBean.getRnPageNew());
                if (listBean.getRnPageNew().contains("evidenceBase")) {
                    ALog.e("onMinViewClick", "跳转医学证据库");
                    ClickUtils.sendEvent(ClickApi.Mine.Mine_evidenceBase, getActivity(), "个人中心医学证据库点击");
                }
                MineUtils.toMediaPageByUrl(listBean.getRnPageNew());
            } else {
                mViewModel.push(getActivity(), listBean.getName(), listBean.getRnModuleNew(), listBean.getRnPageNew(), bundle);
            }
        });
        renzAdapter.setOnItemClickListener((view, position) -> {
            NewMineBean.DataBean.MiddleDTOListBean.IconDTOListBean iconDTOListBean = renzList.get(position);
            if (iconDTOListBean == null) {
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putInt("notEdit", binding.getMyDTO().getPlatformAuditStatus());
            bundle.putBoolean("azUseNew", true);
            if (iconDTOListBean.getAppletFunctionId() > 0) {
                if (iconDTOListBean.getAppletFunctionId() == 3) {
                    getTumor();
                }
            } else {
                mViewModel.push(getActivity(), iconDTOListBean.getName(), iconDTOListBean.getRnModuleNew(), iconDTOListBean.getRnPageNew(), bundle);

            }
        });

        bottomAdapter.setOnItemClickListener((view, position) -> mViewModel.push(getActivity(), bottomList.get(position).getName(), bottomList.get(position).getRnModuleNew(), bottomList.get(position).getRnPageNew(), null));

        ClickUtils.setFastOnClickListener(binding.tvSettingBar, v -> ARouter.getInstance().build(RouterApi.SettingActivity).navigation());

        ClickUtils.setFastOnClickListener(binding.info, v -> {
            if (binding.getMyDTO() == null) return;
            if (TextUtils.isEmpty(binding.getMyDTO().getRnModuleNew())) return;
            if (TextUtils.isEmpty(binding.getMyDTO().getRnPageNew())) return;
            mViewModel.push(getActivity(), "", binding.getMyDTO().getRnModuleNew(), binding.getMyDTO().getRnPageNew(), null);
        });

        ClickUtils.setFastOnClickListener(binding.insurance, v -> {
            if (binding.getMyDTO() == null) return;
            //我的医责险
            Bundle bundle_insurance = new Bundle();
            bundle_insurance.putString("doctorId", binding.getMyDTO().getPassportId() + "");
            mViewModel.push(
                    getActivity(),
                    binding.getMyDTO().getDoctorInsuranceIcon().getName(),
                    binding.getMyDTO().getDoctorInsuranceIcon().getRnModuleNew(),
                    binding.getMyDTO().getDoctorInsuranceIcon().getRnPageNew(),
                    bundle_insurance
            );
        });
    }

    @Override
    public void onClick(View v) {

    }


    void getTumor() {
        mViewModel.getTumor().observe(this, observer -> mViewModel.showTumor(getActivity(), observer.data));
    }
}
